package game.logic.app;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

public class BrickPanel extends JPanel
{
    private final List<PuzzleBrick> bricks = new ArrayList<>();

    public BrickPanel()
    {
        setBackground(Color.decode("#2b2b2b"));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        bricks.addAll(PuzzleBricks.all());
        for (PuzzleBrick brick : bricks)
        {
            brick.setAlignmentX(Component.LEFT_ALIGNMENT);
            add(brick);
        }
    }

    public void removeBrick(PuzzleBrick brick)
    {
        if (bricks.remove(brick))
            remove(brick);
    }

    public void removeAllBricks()
    {
        bricks.clear();
        removeAll();
    }

    public void addBrick(PuzzleBrick brick)
    {
        if (!bricks.contains(brick))
        {
            brick.reset();// 还原成水平状态
            brick.setAlignmentX(Component.LEFT_ALIGNMENT);
            bricks.addFirst(brick);
            add(brick, 0);
        }
    }

    public void addBricks(List<PuzzleBrick> bricks)
    {
        for (PuzzleBrick brick : bricks)
            addBrick(brick);
    }

    public void reset()
    {
        removeAll();

        bricks.clear();
        bricks.addAll(PuzzleBricks.all());
        for (PuzzleBrick brick : bricks)
        {
            brick.setAlignmentX(Component.LEFT_ALIGNMENT);
            add(brick);
        }
    }
}
